class BooksController < ApplicationController
  # GET /books
  # GET /books.json
  def index
    
    destroy_book_session
    destroy_collection_session
    
    @books = Book.all
    @user = current_user

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @books }
    end
  end

  # GET /books/1
  # GET /books/1.json
  def show
    
    destroy_book_session
    destroy_collection_session
    
    @book = Book.find(params[:id])
    @book_ratings = BookRating.joins(:book).where("book_id = ? ", @book.id )
    @collection = Collection.find_by_id(@book.collection_id)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @book }
    end
  end

  # GET /books/new
  # GET /books/new.json
  def new
    
    if admin?
      @book = Book.new
      @collection = current_collection
  
      respond_to do |format|
        format.html # new.html.erb
        format.json { render :json => @book }
      end
    else
      flash.now.alert = "Please, log as admin to manage books"
      respond_to do |format|
        format.html { render "home/index"}  
      end
      return
    end
  end

  # GET /books/1/edit
  def edit
    
    @book = Book.find(params[:id])
    if admin?
      @book
    else  
  #    flash.now.alert = "Please, log as admin to manage books"
      respond_to do |format|
        format.html { redirect_to book_path(@book) , :alert => "Please, log as admin to manage books" }  
      end
      return
    end
  
  end

  # POST /books
  # POST /books.json
  def create
    
    if admin?
    
      @book = Book.new(params[:book])
      if current_collection
        @book.collection_id = current_collection.id
      else
        @book.collection_id = params[:collection][:collection_id]
      end
      
      respond_to do |format|
        if @book.save
          format.html { redirect_to @book, :notice => 'Book was successfully created.' }
          format.json { render :json => @book, :status => :created, :location => @book }
        else
          format.html { render :action => "new" }
          format.json { render :json => @book.errors, :status => :unprocessable_entity }
        end
      end
      
    else
      flash.now.alert = "Please, log as admin to manage books"
      respond_to do |format|
        format.html { render "home/index"}  
      end
      return
    end
      
  end

  # PUT /books/1
  # PUT /books/1.json
  def update
    
    if admin?
    
      @book = Book.find(params[:id])
  
      respond_to do |format|
        if @book.update_attributes(params[:book])
          format.html { redirect_to @book, :notice => 'Book was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render :action => "edit" }
          format.json { render :json => @book.errors, :status => :unprocessable_entity }
        end
      end
    else
      flash.now.alert = "Please, log as admin to manage books"
      respond_to do |format|
        format.html { render "home/index"}  
      end
      return
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    
    if admin?
    
      @book = Book.find(params[:id])
      @book.destroy
  
      respond_to do |format|
        format.html { redirect_to books_url }
        format.json { head :no_content }
      end
    else
      flash.now.alert = "Please, log as admin to manage books"
      respond_to do |format|
        format.html { render "home/index"}  
      end
      return
    end
    
  end
end
